package com.uwantsoft.business.service.domain.addresses;


import com.uwantsoft.business.facade.dto.address.FavoriteAddressesDto;
import com.uwantsoft.business.facade.dto.address.LatLngDto;
import com.uwantsoft.business.service.domain.user.Driver;
import com.uwantsoft.util.AbstractEntity;

import javax.persistence.*;

/**
 * @author LeiPeichen.
 */
@Entity
@Table(name="favoriteaddress")
public class FavoriteAddress extends AbstractEntity {

    @ManyToOne(targetEntity = Driver.class)
    @JoinColumn(name="driver_id")
    private Driver driver;
    @Column(name = "name")
    private String name;
    @Column(name = "address")
    private String address;
    @Column(name = "baidu_po_uid")
    private String baiduPoiUid;
    @Embedded
    private LatLng latLng;

    public String address() {
        return address;
    }

    public FavoriteAddress address(String address) {
        this.address = address;
        return this;
    }
    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }


    public void createFavoriteAddress(Driver driver) {
        this.driver = driver;
    }


    public static FavoriteAddress createFavoriteAddress(FavoriteAddressesDto favoriteAddressesDto) {
        FavoriteAddress favoriteAddress = new FavoriteAddress();
        favoriteAddress.address = favoriteAddressesDto.getAddress();
        favoriteAddress.name = favoriteAddressesDto.getName();
        favoriteAddress.baiduPoiUid = favoriteAddressesDto.getBaiduPoiUid();
        LatLng newLatLng = new LatLng();
        if(favoriteAddressesDto.getLatLng() != null){
            newLatLng.latLng(favoriteAddressesDto.getLatLng().getLatitude(),favoriteAddressesDto.getLatLng().getLongitude());
        }
        favoriteAddress.setLatLng(newLatLng);
        return favoriteAddress;
    }


    public FavoriteAddressesDto convert(){
        FavoriteAddressesDto favoriteAddressesDto = new FavoriteAddressesDto();
        favoriteAddressesDto.setBaiduPoiUid(this.baiduPoiUid);
        LatLngDto latLngDto = new LatLngDto();
        if(this.latLng != null){
            latLngDto.setLatitude(this.latLng.latitude());
            latLngDto.setLongitude(this.latLng.longitude());
        }
        favoriteAddressesDto.setLatLng(latLngDto);
        favoriteAddressesDto.setName(this.name);
        favoriteAddressesDto.setGuid(this.guid());
        favoriteAddressesDto.setAddress(this.address);
        return favoriteAddressesDto;
    }

    /**
     * test
     * @param driver
     * @return
     */
    public static FavoriteAddress testInstanceAddress(Driver driver,FavoriteAddressesDto favoriteAddressesDto){
        FavoriteAddress address = new FavoriteAddress();
        address.driver = driver;
        address.name = favoriteAddressesDto.getName();
        address.address = favoriteAddressesDto.getAddress();
        address.baiduPoiUid = favoriteAddressesDto.getBaiduPoiUid();
        LatLng newLatLng = new LatLng();
        if(favoriteAddressesDto.getLatLng() != null){
            newLatLng.latLng(favoriteAddressesDto.getLatLng().getLatitude(),favoriteAddressesDto.getLatLng().getLongitude());
        }
        address.setLatLng(newLatLng);
        return address;
    }
}
